home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / SCSI Samples 1.0 / SCSI Async Sample 06⁄15 ƒ / Src / SCSIAsyncSampleMain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-16  |  14.1 KB  |  676 lines  |  [TEXT/KAHL]

  1. /*                                ScsiSimpleSample.c                                */
  2. /*
  3.  * ScsiSimpleSample.c
  4.  * Copyright © 1992-93 Apple Computer Inc. All Rights Reserved.
  5.  *
  6.  * Edit history:
  7.  *    1.0        MM        First release
  8.  *    1.0.1    MM        Added dialog for timeout and explicit display of "asych"
  9.  *                    No substantative changes.
  10.  *    1.0.2    MM        Removed references to the message byte field as it is
  11.  *                    no longer returned by the asynchronous SCSI Manager.
  12.  */
  13. #define EXTERN
  14. #include    "SCSIAsyncSample.h"
  15. #include    <Packages.h>
  16. #include    <Desk.h>
  17. #include    <OSEvents.h>
  18. #include    <GestaltEqu.h>
  19. #include    <Errors.h>
  20. #pragma segment MainCode
  21.  
  22. void                                main(void);
  23. void                                EventLoop(void);
  24. void                                DoMouseEvent(void);
  25. void                                DoContentClick(
  26.         WindowPtr                        theWindow
  27.     );
  28. void                                DoCommand(
  29.         WindowPtr                        theWindow,
  30.         long                            menuChoice
  31.     );
  32. void                                DoWindowKeyDown(
  33.         WindowPtr                        theWindow
  34.     );
  35. void                                SetupEverything(void);
  36. void                                AdjustMenus(void);
  37. void                                AdjustEditMenu(
  38.         Boolean                            isDeskAccessory
  39.     );
  40. void                                SetupMenus(void);
  41. void                                BuildWindow(void);
  42. void                                DecorateDisplay(
  43.         WindowPtr                        theWindow,
  44.         Boolean                            redraw
  45.     );
  46. void                                DrawWindow(
  47.         WindowPtr                        theWindow
  48.     );
  49. void                                DoAbout(void);
  50. void                                DoPageSetup(void);
  51. void                                HexToString(
  52.         unsigned long                    value,
  53.         short                            hexDigits,
  54.         Str255                            result
  55.     );
  56. Boolean                                HexToNum(
  57.         ConstStr255Param                hexString,
  58.         unsigned long                    *result
  59.     );
  60.  
  61. void                                SetupAnimatedCursor(
  62.         short                            acurResID
  63.     );
  64. void                                SpinCursor(void);
  65. Boolean                                IsVirtualMemoryRunning(void);
  66.  
  67.  
  68. #define IsOurWindow(theWindow)    ((theWindow) == gMainWindow)
  69.  
  70. void
  71. main(void)
  72. {
  73.         void                        *vmStackTop;
  74.         short                        i;
  75.         
  76.         SetupEverything();
  77.         if (AsyncSCSIPresent() == FALSE
  78.          || GetHostBusCount(&gMaxHostBus) != noErr) {
  79.              InitCursor();
  80.             StopAlert(ALRT_NoNewSCSI, NULL);
  81.             ExitToShell();
  82.         }
  83.         else if (MicrosecondTrapPresent() == FALSE)
  84.             FatalError(unimpErr, "\pNo timer support");
  85.         else {
  86.             GetDevicesToTest();
  87.             BuildWindow();
  88.             DisplayTestParameters();
  89.             SetupAnimatedCursor(ACUR_Animator);
  90.             if (gVirtualMemoryEnabled) {
  91.                 gVMHoldRecord[kVMFunction].ptr = (void *) VMStartMarker;
  92.                 gVMHoldRecord[kVMFunction].size = 
  93.                     ((unsigned long) VMEndMarker) - ((unsigned long) VMStartMarker);
  94.                 gVMHoldRecord[kVMStack].ptr = (void *) &vmStackTop;
  95.                 gVMHoldRecord[kVMStack].size = kVMStackSize;
  96.                 for (i = kVMFunction; i <= kVMStack; i++) {
  97.                     if (HoldMemory(gVMHoldRecord[i].ptr, gVMHoldRecord[i].size) != noErr)
  98.                         gVMHoldRecord[i].ptr = NULL;
  99.                 }
  100.             }
  101.             while (gQuitNow == FALSE) {
  102.                 EventLoop();
  103.             }
  104.         }
  105.         /* KillActiveDevices */
  106.         for (i = kVMFunction; i <= kVMStack; i++) {
  107.             if (gVMHoldRecord[i].ptr != NULL)
  108.                 UnholdMemory(gVMHoldRecord[i].ptr, gVMHoldRecord[i].size);
  109.         }
  110.         /* Release memory */
  111.         ExitToShell();
  112. }
  113.  
  114. void
  115. BuildWindow()
  116. {
  117.         short                    fontSize;
  118.         short                    fontNumber;
  119.         Rect                    viewRect;
  120.         
  121.         fontSize = 10;
  122.         GetFNum("\pCourier", &fontNumber);
  123.         if (RealFont(fontNumber, fontSize) == FALSE)
  124.             fontNumber = applFont;
  125.         viewRect = qd.screenBits.bounds;
  126.         viewRect.top += (GetMBarHeight() * 2);
  127.         viewRect.bottom -= 4;
  128.         viewRect.left += 4;
  129.         viewRect.right = (width(viewRect) / 2);
  130.         gMainWindow = NewWindow(
  131.                         NULL,
  132.                         &viewRect,
  133.                         "\pSCSI Simple Sample",
  134.                         TRUE,
  135.                         zoomDocProc,
  136.                         (WindowPtr) -1L,
  137.                         TRUE,                /* Has GoAway box        */
  138.                         0                    /* No refCon            */
  139.                     );
  140.         if (gMainWindow == NULL) {
  141.             SysBeep(10);
  142.             ExitToShell();
  143.         }
  144.         SetPort(gMainWindow);
  145.         viewRect = gMainWindow->portRect;
  146.         viewRect.right -= kScrollBarOffset;
  147.         viewRect.bottom -= kScrollBarOffset;
  148.         gLogListHandle = CreateLog(
  149.                     &viewRect,
  150.                     fontNumber,
  151.                     fontSize,
  152.                     kLogLines
  153.                 );
  154.         if (gLogListHandle == NULL) {
  155.             SysBeep(10);
  156.             ExitToShell();
  157.         }
  158. }
  159.  
  160. void
  161. EventLoop(void)
  162. {
  163.         long                            menuChoice;
  164.         register WindowPtr                theWindow;
  165.         GrafPtr                            savePort;
  166.         Boolean                            isActivating;
  167.         static Boolean                    testingStarted = FALSE;
  168.         
  169.         if (gUpdateMenusNeeded) {
  170.             gUpdateMenusNeeded = FALSE;
  171.             AdjustMenus();
  172.         }
  173.         WaitNextEvent(
  174.             everyEvent,
  175.             &EVENT,
  176.             (gInForeground) ? 10L : 60L,
  177.             NULL
  178.         );
  179.         theWindow = FrontWindow();
  180.         switch (EVENT.what) {
  181.         case nullEvent:
  182.             break;
  183.         case keyDown:
  184.         case autoKey:
  185.             if ((EVENT.message & charCodeMask) == '.'
  186.              && (EVENT.modifiers & cmdKey) != 0) {
  187.                 FlushEvents(keyDown | autoKey, 0);
  188.                 gStopNow = TRUE;
  189.             }
  190.             else if ((EVENT.modifiers & cmdKey) != 0) {
  191.                 if (EVENT.what == keyDown) {
  192.                     menuChoice = MenuKey(EVENT.message & charCodeMask);
  193.                     if (HiWord(menuChoice) != 0)
  194.                         DoCommand(theWindow, menuChoice);
  195.                     else if (IsOurWindow(theWindow)) {
  196.                         DoWindowKeyDown(theWindow);
  197.                     }
  198.                 }
  199.             }
  200.             else if (IsOurWindow(theWindow)) {
  201.                 DoWindowKeyDown(theWindow);
  202.             }
  203.             break;
  204.         case mouseDown:
  205.             DoMouseEvent();
  206.             break;
  207.         case updateEvt:
  208.             theWindow = (WindowPtr) EVENT.message;
  209.             GetPort(&savePort);
  210.             SetPort(theWindow);
  211.             BeginUpdate(theWindow);
  212.             EraseRect(&theWindow->portRect);
  213.             DrawControls(theWindow);
  214.             DrawGrowIcon(theWindow);
  215.             if (IsOurWindow(theWindow))
  216.                 DrawWindow(theWindow);
  217.             EndUpdate(theWindow);
  218.             SetPort(savePort);
  219.             break;
  220.         case activateEvt:
  221.             theWindow = (WindowPtr) EVENT.message;
  222.             isActivating = ((EVENT.modifiers & activeFlag) != 0);
  223.             goto activateEvent;
  224.             break;
  225.         case osEvt:
  226.             switch (((unsigned long) EVENT.message) >> 24) {
  227.             case mouseMovedMessage:
  228.                 break;
  229.             case suspendResumeMessage:
  230.                 isActivating = ((EVENT.message & 0x01) != 0);
  231. activateEvent:        if (isActivating) {
  232.                     /*
  233.                      * Activate this window. Activate events define theWindow
  234.                      * from the event record, while suspend/resume uses the
  235.                      * pre-set FrontWindow value.
  236.                      */
  237.                     SelectWindow(theWindow);
  238.                     (void) TEFromScrap();
  239.                 }
  240.                 if (IsOurWindow(theWindow) && gLogListHandle != NULL)
  241.                     LActivate(isActivating, gLogListHandle);
  242.                 else {
  243.                     /* Desk accessory or what? */
  244.                 }
  245.                 gInForeground = isActivating;
  246.                 gUpdateMenusNeeded = TRUE;
  247.                 break;
  248.             }
  249.             break;
  250.         }
  251.         if (gStopNow == FALSE && gQuitNow == FALSE) {
  252.             SpinCursor();
  253.             if (testingStarted)
  254.                 ContinueTesting();
  255.             else {
  256.                 testingStarted = TRUE;
  257.                 StartTesting();
  258.             }
  259.         }
  260. }
  261.  
  262. /*
  263.  * DoMouseEvent
  264.  * The user clicked on something. Handle application-wide processing here, or call
  265.  * a Catalog Browser function for specific action.
  266.  */
  267. void
  268. DoMouseEvent(void)
  269. {
  270.         WindowPtr        theWindow;
  271.         short            whichPart;
  272.         
  273.         whichPart = FindWindow(EVENT.where, &theWindow);
  274.         if (theWindow == NULL)
  275.             theWindow = FrontWindow();
  276.         if (whichPart == inMenuBar && IsOurWindow(theWindow) == FALSE)
  277.             theWindow = FrontWindow();
  278.         switch (whichPart) {
  279.         case inDesk:
  280.             break;
  281.         case inMenuBar:
  282.             InitCursor();
  283.             DoCommand(theWindow, MenuSelect(EVENT.where));
  284.             break;
  285.         case inDrag:
  286.             DragWindow(theWindow, EVENT.where, &qd.screenBits.bounds);
  287.             break;
  288.         case inGoAway:
  289.             if (TrackGoAway(theWindow, EVENT.where)) {
  290.                 if (IsOurWindow(theWindow)) {
  291.                     /*
  292.                      * Not quite so simple: we need to handle open files, too.
  293.                      */
  294.                     gQuitNow = TRUE;
  295.                 }
  296.             }
  297.             break;
  298.         case inZoomIn:
  299.         case inZoomOut:
  300.             if (IsOurWindow(theWindow)
  301.              && TrackBox(theWindow, EVENT.where, whichPart)) {
  302.                 DoZoomWindow(theWindow, whichPart);
  303.                 goto resizeWindow;
  304.             }
  305.             break;
  306.         case inGrow:
  307.             if (IsOurWindow(theWindow)) {
  308.                 if (DoGrowWindow(
  309.                             theWindow,
  310.                             EVENT.where,
  311.                             kMinWindowWidth,
  312.                             kMinWindowHeight
  313.                         )) {
  314. resizeWindow:        DecorateDisplay(theWindow, TRUE);
  315.                 }
  316.             }
  317.             break;
  318.         case inContent:
  319.             if (theWindow != FrontWindow())
  320.                 SelectWindow(theWindow);
  321.             else if (IsOurWindow(theWindow)) {
  322.                 DoContentClick(theWindow);
  323.             }
  324.             else {
  325.                 /* Nothing happens here        */
  326.             }
  327.             break;
  328.         default:
  329.             break;                        /* Might be for the SDP Panel    */
  330.         }
  331.         /*
  332.          * Do not touch theWindow here.
  333.          */
  334. }
  335.  
  336. void
  337. DoContentClick(
  338.         WindowPtr                theWindow
  339.     )
  340. {
  341.         if (0) {
  342.             theWindow;
  343.         }
  344.         DoClickInLog(gLogListHandle, &EVENT);
  345.         /*
  346.          * More to follow.
  347.          */
  348. }
  349.  
  350. void
  351. DoWindowKeyDown(
  352.         WindowPtr                        theWindow
  353.     )
  354. {
  355.         if (0) {
  356.             theWindow;
  357.         }
  358.         /* Nothing happens here */
  359. }
  360.  
  361. void
  362. DrawWindow(
  363.         WindowPtr                        theWindow
  364.     )
  365. {
  366.         if (0) {
  367.             theWindow;
  368.         }
  369.         UpdateLog(gLogListHandle);
  370. }
  371.  
  372. void
  373. DecorateDisplay(
  374.         WindowPtr                        theWindow,
  375.         Boolean                            redraw
  376.     )
  377. {
  378.         Rect                            viewRect;
  379.         
  380.         if (0) {
  381.             redraw;
  382.         }
  383.         viewRect = theWindow->portRect;
  384.         viewRect.right -= kScrollBarOffset;
  385.         viewRect.bottom -= kScrollBarOffset;
  386.         if (gLogListHandle != NULL) {
  387.             MoveLog(gLogListHandle, viewRect.left, viewRect.top);
  388.             SizeLog(gLogListHandle, width(viewRect), height(viewRect));
  389.         }
  390. }
  391.  
  392. void
  393. DoCommand(
  394.         WindowPtr                        theWindow,
  395.         long                            menuChoice
  396.     )
  397. {
  398.         short                            menuItem;
  399.         Str255                            menuText;
  400.         GrafPtr                            savePort;
  401.         OSErr                            status;
  402.  
  403.         menuItem = LoWord(menuChoice);
  404.         switch (HiWord(menuChoice)) {
  405.         case MENU_Apple:
  406.             if (menuItem == kAppleAbout)
  407.                 DoAbout();
  408.             else {
  409.                 GetItem(gAppleMenu, menuItem, menuText);
  410.                 AdjustEditMenu(TRUE);
  411.                 GetPort(&savePort);
  412.                 OpenDeskAcc(menuText);
  413.                 SetPort(savePort);
  414.                 AdjustEditMenu(IsOurWindow(theWindow) == FALSE);
  415.             }
  416.             break;
  417.         case MENU_File:
  418.             switch (menuItem) {
  419.             case kFileCreateLogFile:
  420.                 status = SaveLogFile(
  421.                             gLogListHandle,
  422.                             "\pSave Log",
  423.                             "\pSCSI Test Log",
  424.                             'ttxt'
  425.                         );
  426.                 break;
  427.             case kFileCloseLogFile:
  428.                 status = CloseLogFile(gLogListHandle);
  429.                 break;
  430.             case kFilePageSetup:
  431.                 DoPageSetup();
  432.                 break;
  433.             case kFilePrint:
  434.                 PrintLog(gLogListHandle, gPrintHandle);
  435.                 break;
  436.             case kFileDebug:
  437.                 Debugger();
  438.                 break;
  439.             case kFileQuit:
  440.                 gQuitNow = TRUE;
  441.                 break;
  442.             }
  443.             break;
  444.         }
  445.         HiliteMenu(0);
  446. }
  447.  
  448. void
  449. SetupEverything()
  450. {
  451.         int                    i;
  452.         
  453.         MaxApplZone();
  454.         InitGraf(&qd.thePort);
  455.         InitFonts();
  456.         InitWindows();
  457.         InitMenus();
  458.         TEInit();
  459.         InitDialogs(0);
  460.         for (i = 0; i < 8; i++)
  461.             MoreMasters();
  462.         HNoPurge((Handle) GetCursor(watchCursor));
  463.         SetCursor(*GetCursor(watchCursor));
  464.         SetupMenus();
  465.         gVirtualMemoryEnabled = IsVirtualMemoryRunning();
  466. }
  467.  
  468. void
  469. SetupMenus()
  470. {
  471.         register Handle        menuBarHdl;
  472.  
  473.         /*
  474.          * We ought to do some error checking here.
  475.          */
  476.         menuBarHdl = GetNewMBar(MBAR_MenuBar);
  477.         SetMenuBar(menuBarHdl);
  478.         gAppleMenu = GetMHandle(MENU_Apple);
  479.         AddResMenu(gAppleMenu, 'DRVR');
  480.         gFileMenu = GetMHandle(MENU_File);
  481.         gEditMenu = GetMHandle(MENU_Edit);
  482.         DrawMenuBar();
  483.         gUpdateMenusNeeded = TRUE;
  484. }
  485.  
  486. void
  487. AdjustMenus(void)
  488. {
  489.         EnableItem(gFileMenu, kFileQuit);
  490.         EnableItem(gFileMenu, kFileDebug);
  491.         if (IsOurWindow(FrontWindow())) {
  492.             EnableItem(gFileMenu, kFilePageSetup);
  493.             EnableItem(gFileMenu, kFilePrint);
  494.             if (HasLogFile(gLogListHandle)) {
  495.                 EnableItem(gFileMenu, kFileCloseLogFile);
  496.                 DisableItem(gFileMenu, kFileCreateLogFile);
  497.             }
  498.             else {
  499.                 EnableItem(gFileMenu, kFileCreateLogFile);
  500.                 DisableItem(gFileMenu, kFileCloseLogFile);
  501.             }
  502.         }
  503.         AdjustEditMenu(IsOurWindow(FrontWindow()) == FALSE);
  504. }
  505.  
  506. /*
  507.  * AdjustEditMenu
  508.  * Enable/disable Edit Menu options.
  509.  */
  510. void
  511. AdjustEditMenu(
  512.         Boolean                isDeskAcc
  513.     )
  514. {
  515.         if (isDeskAcc) {
  516.             EnableItem(gEditMenu, kEditUndo);
  517.             EnableItem(gEditMenu, kEditCut);
  518.             EnableItem(gEditMenu, kEditCopy);
  519.             EnableItem(gEditMenu, kEditPaste);
  520.             EnableItem(gEditMenu, kEditClear);
  521.         }
  522.         else {
  523.             DisableItem(gEditMenu, kEditUndo);
  524.             DisableItem(gEditMenu, kEditCut);
  525.             DisableItem(gEditMenu, kEditCopy);
  526.             DisableItem(gEditMenu, kEditPaste);
  527.             DisableItem(gEditMenu, kEditClear);
  528.         }
  529. }
  530.  
  531. void
  532. DoAbout()
  533. {
  534.         GrafPtr                            savePort;
  535.         DialogPtr                        dialog;
  536.         short                            item;
  537.         
  538.         GetPort(&savePort);
  539.         dialog = GetNewDialog(DLOG_About, NULL, (WindowPtr) -1L);
  540.         ShowWindow(dialog);
  541.         SetPort(dialog);
  542.         ModalDialog(NULL, &item);
  543.         DisposDialog(dialog);
  544.         SetPort(savePort);
  545. }
  546.  
  547. void
  548. HexToString(
  549.         unsigned long                    value,
  550.         short                            hexDigits,
  551.         Str255                            result
  552.     )
  553. {
  554.         register unsigned short            digit;
  555.         register char                    *text;
  556.         
  557.         text = (char *) result;
  558.         text[0] = hexDigits;
  559.         for (; hexDigits > 0; --hexDigits) {
  560.             digit = value & 0xF;
  561.             if (digit >= 10)
  562.                 text[hexDigits] = digit - 10 + 'a';
  563.             else {
  564.                 text[hexDigits] = digit + '0';
  565.             }
  566.             value >>= 4;
  567.         }
  568. }
  569.  
  570. Boolean
  571. HexToNum(
  572.         ConstStr255Param                hexString,
  573.         unsigned long                    *result
  574.     )
  575. {
  576.         register short                    i;
  577.         register unsigned short            digit;
  578.         
  579.         *result = 0;
  580.         for (i = 1; i <= hexString[0]; i++) {
  581.             digit = hexString[i];
  582.             if (digit >= '0' && digit <= '9')
  583.                 digit -= '0';
  584.             else if (digit >= 'a' && digit <= 'f')
  585.                 digit = digit - 'a' + 10;
  586.             else if (digit >= 'A' && digit <= 'F')
  587.                 digit = digit - 'A' + 10;
  588.             else {
  589.                 return (FALSE);
  590.             }
  591.             *result <<= 4;
  592.             *result |= digit;
  593.         }
  594.         return (TRUE);
  595. }
  596.  
  597. void
  598. DoPageSetup(void)
  599. {
  600.         PrOpen();
  601.         if (PrError() == noErr) {
  602.             if (gPrintHandle == NULL) {
  603.                 gPrintHandle = (THPrint) NewHandle(sizeof (TPrint));
  604.                 if (gPrintHandle != NULL)
  605.                     PrintDefault(gPrintHandle);
  606.             }
  607.             if (gPrintHandle != NULL)
  608.                 (void) PrStlDialog(gPrintHandle);
  609.             PrClose();
  610.         }
  611. }
  612.  
  613. void
  614. NonFatalError(
  615.         OSErr                    errorStatus,
  616.         ConstStr255Param        errorMsg
  617.     )
  618. {
  619.         Handle                    errorTextHdl;
  620.         StringPtr                errorTextPtr;
  621.         Str15                    errorStatusText;
  622.         short                    userChoice;
  623.         
  624.         NumToString(errorStatus, errorStatusText);
  625.         errorTextHdl = GetResource('Estr', errorStatus);
  626.         if (errorTextHdl != NULL) {
  627.             HLock(errorTextHdl);
  628.             errorTextPtr = (StringPtr) *errorTextHdl;
  629.         }
  630.         else {
  631.             errorTextPtr = "\pSystem Error";
  632.         }
  633.         ParamText(errorStatusText, errorTextPtr, errorMsg, "\p");
  634.         userChoice = StopAlert(ALRT_NonFatalError, NULL);
  635.         if (userChoice == 1)
  636.             ExitToShell();
  637. }
  638.  
  639.  
  640. void
  641. FatalError(
  642.         OSErr                errorStatus,
  643.         ConstStr255Param    errorMsg
  644.     )
  645. {
  646.         Handle                errorTextHdl;
  647.         StringPtr            errorTextPtr;
  648.         Str15                errorStatusText;
  649.         
  650.         NumToString(errorStatus, errorStatusText);
  651.         errorTextHdl = GetResource('Estr', errorStatus);
  652.         if (errorTextHdl != NULL) {
  653.             HLock(errorTextHdl);
  654.             errorTextPtr = (StringPtr) *errorTextHdl;
  655.         }
  656.         else {
  657.             errorTextPtr = "\pSystem Error";
  658.         }
  659.         ParamText(errorStatusText, errorTextPtr, errorMsg, "\p");
  660.         StopAlert(ALRT_FatalError, NULL);
  661.         ExitToShell();
  662. }
  663.  
  664. Boolean
  665. IsVirtualMemoryRunning(void)
  666. {
  667.         OSErr                        status;
  668.         long                        response;
  669.         
  670.         status = Gestalt(gestaltVMAttr, &response);
  671.         /*
  672.          * VM is active iff Gestalt succeeded and the response is appropriate.
  673.          */
  674.         return (status == noErr && ((response & (1 << gestaltVMPresent)) != 0));
  675. }
  676.